#include "database.h"

sqlite3 *pd = NULL;
char *errmsg = NULL;

int initDatabase()
{
    int ret = -1;

    ret = sqlite3_open("MeituanData.db", &pd);
    if (ret != 0)
    {
        fprintf(stderr, "无法打开数据库：%s\n", sqlite3_errmsg(pd));
        if (pd)
            sqlite3_close(pd);
        return -1;
    }
    printf("成功创建/打开数据库文件\n");
    return 1;
}

int insert_sql(RGU *rgu, char *s)
{
    char table_name[20];
    char insert_sql[512];

    strcpy(table_name, s);

    if (0 == strcmp(table_name, "riderRegister"))
    {
        sprintf(insert_sql,
                "INSERT INTO %s (Name,Password,Phone) VALUES('%s','%s','%s')",
                table_name, rgu->name, rgu->pwd, rgu->phone);
    }
    else
    {
        sprintf(insert_sql,
                "INSERT INTO %s (Name,Password,Phone,Address) VALUES('%s','%s','%s','%s')",
                table_name, rgu->name, rgu->pwd, rgu->phone, rgu->address);
    }

    int ret = sqlite3_exec(pd, insert_sql, 0, 0, &errmsg);
    if (ret != 0)
    {
        fprintf(stderr, "SQL 错误: %s\n", errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    printf("注册成功！\n");
}

int AddDish_sql(MENU *rgu, char *s)
{
    char table_name[20];
    char add_sql[512];

    strcpy(table_name, s);

    sprintf(add_sql,
            "INSERT INTO %s (storeID,dishName,Price,Sales) VALUES('%s','%s','%.2f','%d')",
            table_name, rgu->shoreID, rgu->dishName, rgu->Price, rgu->Sales);

    int ret = sqlite3_exec(pd, add_sql, 0, 0, &errmsg);
    if (errmsg != NULL)
    {
        printf("增加数据库失败，SQL语句是：%s 原因是：%s\n", add_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
    }
    printf("添加菜品成功！\n");
}

int CurOrder_sql(MENU *rgu, char *s1, char *s2)
{
    char table_name[20];
    char table_name02[20];
    char add_sql[512];
    char add_sql02[512];

    strcpy(table_name, s1);
    strcpy(table_name02, s2);

    // 插入订单表
    sprintf(add_sql,
            "INSERT INTO %s (shoreName, dishName, amount, totalPrice, client, clientPhone, clientAddress) "
            "VALUES ('%s', '%s', %d, %.2f, '%s', '%s', '%s');",
            table_name, rgu->shoreID, rgu->dishName, rgu->Sales, rgu->Price, rgu->ClientName, rgu->ClientPhone, rgu->ClientAddress);
    printf("444444%s\n", rgu->shoreID);
    // 执行插入操作
    int ret = sqlite3_exec(pd, add_sql, 0, 0, &errmsg);

    if (ret != SQLITE_OK)
    {
        printf("增加数据库失败，SQL语句是：%s 原因是：%s\n", add_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    printf("555555%s\n", rgu->shoreID);
    printf("添加订单成功！\n");

    // 插入历史订单表
    sprintf(add_sql02,
            "INSERT INTO %s (storeName, dishName, amount, totalPrice, client, clientPhone, clientAddress) "
            "VALUES ('%s', '%s', %d, %.2f, '%s', '%s', '%s');",
            table_name02, rgu->shoreID, rgu->dishName, rgu->Sales, rgu->Price, rgu->ClientName, rgu->ClientPhone, rgu->ClientAddress);
    printf("66666%s\n", rgu->shoreID);
    ret = sqlite3_exec(pd, add_sql02, 0, 0, &errmsg);
    if (ret != SQLITE_OK)
    {
        printf("增加历史订单失败，SQL语句是：%s 原因是：%s\n", add_sql02, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    printf("777777%s\n", rgu->shoreID);
    printf("添加历史订单成功！\n");

    return 1;
}

int Delete_sql(MENU *rgu, char *s)
{
    char table_name[20];
    char delete_sql[512];

    strcpy(table_name, s);

    sprintf(delete_sql,
            "DELETE FROM %s WHERE storeID = '%s' AND dishName = '%s'", table_name, rgu->shoreID, rgu->dishName);

    int ret = sqlite3_exec(pd, delete_sql, 0, 0, &errmsg);
    if (errmsg != NULL)
    {
        printf("删除数据库失败，SQL语句是：%s 原因是：%s\n", delete_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
    }
}

int Modify_sql(MENU *rgu, char *s)
{
    char table_name[20];
    char modify_sql[512];

    strcpy(table_name, s);

    sprintf(modify_sql,
            "UPDATE %s SET dishName = '%s',Price = '%.2f' WHERE storeID = '%s' AND dishName = '%s';", table_name, rgu->NewDishName, rgu->Price, rgu->shoreID, rgu->dishName);

    int ret = sqlite3_exec(pd, modify_sql, 0, 0, &errmsg);
    if (errmsg != NULL)
    {
        printf("修改数据库失败，SQL语句是：%s 原因是：%s\n", modify_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
    }
}

int show_Sql(QUREY_RST *pQR, MENU *rgu, char *s)
{
    char table_name[20];
    strcpy(table_name, s);
    char show_sql[256];
    sprintf(show_sql, "select * from %s where storeID = '%s';", table_name, rgu->shoreID);

    int data = sqlite3_get_table(pd, show_sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);
    if (errmsg != NULL)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", show_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
    }
}

int findClient_Sql(QUREY_RST *pQR, MENU *rgu, char *s)
{
    char table_name[20];
    strcpy(table_name, s);
    char show_sql[256];
    sprintf(show_sql, "SELECT Phone, Address FROM %s WHERE Name = '%s';", table_name, rgu->ClientName);
    int data = sqlite3_get_table(pd, show_sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);
    if (errmsg != NULL)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", show_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        if (pQR->row > 0)
        {
            // 假设返回的结果集的第一个数据行存储的是 Phone 和 Address
            strcpy(rgu->ClientPhone, pQR->pzResult[pQR->col]);       // pQR->pzResult[0]是 Phone
            strcpy(rgu->ClientAddress, pQR->pzResult[pQR->col + 1]); // pQR->pzResult[1]是 Address
        }
        return 1;
    }
}

int findClient_Sql02(QUREY_RST *pQR, ORDER_INFOR *oRD, char *s)
{
    char table_name[20];
    strcpy(table_name, s);
    char show_sql[256];
    sprintf(show_sql, "SELECT Phone, Address FROM %s WHERE Name = '%s';", table_name, oRD->clientName);
    int data = sqlite3_get_table(pd, show_sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);
    if (errmsg != NULL)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", show_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        if (pQR->row > 0)
        {
            // 假设返回的结果集的第一个数据行存储的是 Phone 和 Address
            strcpy(oRD->clientPhone, pQR->pzResult[pQR->col]);     // pQR->pzResult[0]是 Phone
            strcpy(oRD->clientPhone, pQR->pzResult[pQR->col + 1]); // pQR->pzResult[1]是 Address
        }
        return 1;
    }
}

int findPending_Sql(QUREY_RST *pQR, char *s, char *shoreName)
{
    char table_name[20];
    strcpy(table_name, s);
    char show_sql[256];
    sprintf(show_sql, "SELECT orderID,dishName,amount,totalPrice,client,clientPhone,clientAddress,orderTime FROM %s WHERE shoreName = '%s';", table_name, shoreName);
    int data = sqlite3_get_table(pd, show_sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);
    if (errmsg != NULL)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", show_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
    }
}

int FindClientOrder_Sql(QUREY_RST *pQR, char *s, int orderID)
{
    char table_name[20];
    strcpy(table_name, s);
    char show_sql[256];
    sprintf(show_sql, "SELECT orderID,client,shoreName,dishName,amount,totalPrice,orderTime FROM %s WHERE orderID = '%d';", table_name, orderID);
    int data = sqlite3_get_table(pd, show_sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);
    if (errmsg != NULL)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", show_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
    }
}

int FindClientOrder_Sql02(QUREY_RST *pQR, char *s, char *merchantName)
{
    char table_name[20];
    strcpy(table_name, s);
    char show_sql[256];
    sprintf(show_sql, "SELECT orderID,clientName,dishName,amount,totalPrice,orderState,orderTime FROM %s WHERE storeName = '%s';", table_name, merchantName);
    int data = sqlite3_get_table(pd, show_sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);
    if (errmsg != NULL)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", show_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
    }
}

int FindClientOrder_Sql03(QUREY_RST *pQR, char *s, char *clientName)
{
    char table_name[20];
    strcpy(table_name, s);
    char show_sql[256];
    sprintf(show_sql, "SELECT orderID,storeName,dishName,amount,totalPrice,orderState,orderTime FROM %s WHERE clientName = '%s';", table_name,clientName);

    int data = sqlite3_get_table(pd, show_sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);
    if (errmsg != NULL)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", show_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
    }
}

int InsertClientOrder_Sql(QUREY_RST *pQR, char *table_name02, char *dealingOrder)
{
    char insert_sql[512];
    int i;

    // 遍历查询结果并插入到 ClientOrders 表中
    for (i = 1; i <= pQR->row; i++)
    {                                                              // pQR->row是查询到的行数，跳过第一行（表头）
        int orderID = atoi(pQR->pzResult[i * pQR->col]);           // 订单ID
        char *clientName = pQR->pzResult[i * pQR->col + 1];        // 客户名
        char *shoreName = pQR->pzResult[i * pQR->col + 2];         // 商家名
        char *dishName = pQR->pzResult[i * pQR->col + 3];          // 菜品名
        int amount = atoi(pQR->pzResult[i * pQR->col + 4]);        // 数量
        double totalPrice = atof(pQR->pzResult[i * pQR->col + 5]); // 总价
        char *orderTime = pQR->pzResult[i * pQR->col + 6];         // 订单时间

        // 创建插入 SQL 语句
        snprintf(insert_sql, sizeof(insert_sql),
                 "INSERT INTO %s (orderID, clientName, storeName, dishName, amount, totalPrice,orderState, orderTime) "
                 "VALUES (%d, '%s', '%s', '%s', %d, %.2f, '%s','%s');",
                 table_name02, orderID, clientName, shoreName, dishName, amount, totalPrice, dealingOrder, orderTime);

        // 执行 SQL 插入
        int rc = sqlite3_exec(pd, insert_sql, 0, 0, &errmsg);
        if (rc != SQLITE_OK)
        {
            printf("插入数据失败，SQL语句是：%s 原因是：%s\n", insert_sql, errmsg);
            sqlite3_free(errmsg);
            return -1;
        }
    }
    return 1; // 返回成功
}

int UpdateClientOrder_Sql01(QUREY_RST *pQR, char *table_name, int orderID)
{
    char show_sql[256];
    sprintf(show_sql, "SELECT orderID, clientName, storeName, dishName, amount, totalPrice FROM %s WHERE orderID = %d;", table_name, orderID);

    // 执行查询
    int data = sqlite3_get_table(pd, show_sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);

    if (errmsg != NULL)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", show_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }

    return 1;
}

int UpdateClientOrder_Sql02(QUREY_RST *pQR, ORDER_INFOR *oRD, char *table_name, char *dealingOrder)
{
    printf("%s\n", dealingOrder);
    for (int i = 1; i <= 1; i++)
    {
        // 获取查询到的第一个数据行
        int orderID = atoi(pQR->pzResult[i * pQR->col]); // orderID
        printf("%d\n", orderID);
        char *clientName = pQR->pzResult[i * pQR->col + 1]; // clientName
        printf("%s\n", clientName);
        char *shoreName = pQR->pzResult[i * pQR->col + 2]; // shoreName
        printf("%s\n", shoreName);
        char *dishName = pQR->pzResult[i * pQR->col + 3]; // dishName
        printf("%s\n", dishName);
        int amount = atoi(pQR->pzResult[i * pQR->col + 4]); // amount
        printf("%d\n", amount);
        double totalPrice = atof(pQR->pzResult[i * pQR->col + 5]); // totalPrice
        printf("%.2f\n", totalPrice);

        // 构建插入SQL语句
        char insert_sql[512];
        snprintf(insert_sql, sizeof(insert_sql),
                 "INSERT INTO %s (orderID, clientName, storeName, dishName, amount, totalPrice, orderState) "
                 "VALUES ('%d', '%s', '%s', '%s', %d, %.2f,'%s');",
                 table_name, orderID, clientName, shoreName, dishName, amount, totalPrice, dealingOrder);

        oRD->orderID = orderID;
        strcpy(oRD->clientName, clientName);
        strcpy(oRD->storeName, shoreName);
        strcpy(oRD->dishName, dishName);
        oRD->amount = amount;
        oRD->totalPrice = totalPrice;
        strcpy(oRD->orderState, dealingOrder);
        // 执行插入操作
        int rc = sqlite3_exec(pd, insert_sql, 0, 0, &errmsg);

        printf("%s\n", dealingOrder);
        if (rc != SQLITE_OK)
        {
            printf("插入数据失败，SQL语句是：%s 原因是：%s\n", insert_sql, errmsg);
            sqlite3_free(errmsg);
            return -1;
        }
        printf("插入数据成功！\n");
        return 1;
    }
}

int insertChatRecords_sql(SINGLE_CHAT *sc, char *s) {
    char table_name[50];  // 表名缓冲区，增加长度，防止溢出
    if (strlen(s) >= sizeof(table_name)) {
        printf("表名过长\n");
        return -1;
    }
    strcpy(table_name, s);

    // 构造 SQL 语句，不直接插入数据
    char insertChats_sql[1024];
    snprintf(insertChats_sql, sizeof(insertChats_sql),
             "INSERT INTO %s (destName, selfName, chatText) VALUES (?, ?, ?);", 
             table_name);

    // 准备 SQL 语句
    sqlite3_stmt *stmt;
    int rc = sqlite3_prepare_v2(pd, insertChats_sql, -1, &stmt, NULL);
    if (rc != SQLITE_OK) {
        printf("准备 SQL 语句失败: %s\n", sqlite3_errmsg(pd));
        return -1;
    }

    // 绑定参数
    sqlite3_bind_text(stmt, 1, sc->destName, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 2, sc->selfName, -1, SQLITE_STATIC);
    sqlite3_bind_text(stmt, 3, sc->text, -1, SQLITE_STATIC);

    // 执行插入操作
    rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE) {
        printf("插入数据失败: %s\n", sqlite3_errmsg(pd));
        sqlite3_finalize(stmt);
        return -1;
    }

    // 释放准备的语句
    sqlite3_finalize(stmt);
    return 1;
}


// int insertChatRecords_sql(SINGLE_CHAT *sc, char *s)
// {
//     char table_name[20];
//     strcpy(table_name, s);
//     char insertChats_sql[1024];
//     snprintf(insertChats_sql, sizeof(insertChats_sql),
//              "INSERT INTO %s (destName,selfName,chatText) "
//              "VALUES ('%s', '%s', '%s');",
//              table_name, sc->destName, sc->selfName, sc->text);

//         // 执行 SQL 插入
//         int rc = sqlite3_exec(pd, insertChats_sql, 0, 0, &errmsg);
//         if (rc != SQLITE_OK)
//         {
//             printf("插入数据失败，SQL语句是：%s 原因是：%s\n", insertChats_sql, errmsg);
//             sqlite3_free(errmsg);
//             return -1;
//         }
    

//     return 1; // 返回成功

// }


int FindSelfChats_Sql(QUREY_RST *pQR, char *table_name, char *selfName)
{
    char show_sql[256];
    sprintf(show_sql, "SELECT selfName,chatText,chatTime FROM %s WHERE destName = '%s';", table_name, selfName);

    // 执行查询
    int data = sqlite3_get_table(pd, show_sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);
    printf("1111111111111:%d\n",pQR->row);
    printf("222222222222::%s\n",selfName);
    if (errmsg != NULL)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", show_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }

    return 1;
}

int DeleteSelfChats_Sql(char *s, char* selfName)
{
    char table_name[20];
    strcpy(table_name, s);
    char deleteID_sql[256];
    sprintf(deleteID_sql, "DELETE FROM %s WHERE  destName= '%s';", table_name, selfName);
    int ret = sqlite3_exec(pd, deleteID_sql, 0, 0, &errmsg);
    if (errmsg != NULL)
    {
        printf("修改数据库失败，SQL语句是：%s 原因是：%s\n", deleteID_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
    }
}

int deletePending_Sql(char *s, int orderID)
{
    char table_name[20];
    strcpy(table_name, s);
    char deleteID_sql[256];
    sprintf(deleteID_sql, "DELETE FROM %s WHERE orderID = '%d';", table_name, orderID);
    int ret = sqlite3_exec(pd, deleteID_sql, 0, 0, &errmsg);
    if (errmsg != NULL)
    {
        printf("修改数据库失败，SQL语句是：%s 原因是：%s\n", deleteID_sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
    }
}

int FindClientName_Sql(char *s, char *clientName, int orderID)
{
    sqlite3_stmt *stmt;
    char sql[256];
    char table_name[50];
    int ret;

    strcpy(table_name, s);

    // 拼接 SQL 查询语句
    snprintf(sql, sizeof(sql), "SELECT clientName FROM %s WHERE id = %d;", table_name, orderID);

    // 准备执行 SQL 语句
    ret = sqlite3_prepare_v2(pd, sql, -1, &stmt, NULL);
    if (ret != SQLITE_OK)
    {
        printf("准备 SQL 语句失败: %s\n", sqlite3_errmsg(pd));
        return -1;
    }

    // 执行查询并获取结果
    ret = sqlite3_step(stmt);
    if (ret == SQLITE_ROW)
    {
        // 获取查询结果中的 clientName 列
        const char *name = (const char *)sqlite3_column_text(stmt, 0); // 0 表示第一列
        if (name != NULL)
        {
            strcpy(clientName, name); // 将查询结果复制到 client_name 变量
        }
    }
    else
    {
        printf("查询失败: %s\n", sqlite3_errmsg(pd));
        sqlite3_finalize(stmt);
        return -1;
    }

    // 清理 SQL 语句
    sqlite3_finalize(stmt);
    return 0;
}

int order_Sql(MENU *rgu, char *s, double *price)
{
    sqlite3_stmt *stmt;
    char table_name[20];
    strcpy(table_name, s);
    char sql[256];

    // 修正：UPDATE语句使用显式加法操作
    snprintf(sql, sizeof(sql), "UPDATE %s SET Sales = Sales + ? WHERE storeID = ? AND dishName = ?;", table_name);

    // 准备SQL语句
    if (sqlite3_prepare_v2(pd, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        printf("准备SQL失败，错误信息：%s\n", sqlite3_errmsg(pd));
        return -1;
    }

    // 绑定参数
    sqlite3_bind_int(stmt, 1, rgu->Sales);                        // 绑定Sales的值
    sqlite3_bind_text(stmt, 2, rgu->shoreID, -1, SQLITE_STATIC);  // 绑定storeID
    sqlite3_bind_text(stmt, 3, rgu->dishName, -1, SQLITE_STATIC); // 绑定dishName

    // 执行SQL
    int rc = sqlite3_step(stmt);
    if (rc != SQLITE_DONE)
    {
        printf("更新Sales失败，错误信息：%s\n", sqlite3_errmsg(pd));
        sqlite3_finalize(stmt);
        return -1;
    }

    // 清理SQL语句
    sqlite3_finalize(stmt);

    // 修正：查询价格的SQL语句
    snprintf(sql, sizeof(sql), "SELECT Price FROM Menu WHERE storeID = '%s' AND dishName = '%s';",
             rgu->shoreID, rgu->dishName);

    // 准备SQL语句
    if (sqlite3_prepare_v2(pd, sql, -1, &stmt, NULL) != SQLITE_OK)
    {
        printf("准备查询价格SQL失败，错误信息：%s\n", sqlite3_errmsg(pd));
        return -1;
    }

    // 执行查询并获取价格
    if (sqlite3_step(stmt) == SQLITE_ROW)
    {
        *price = sqlite3_column_double(stmt, 0); // 获取Price
        *price *= rgu->Sales;                    // 计算需支付金额（销售量 * 单价）
    }
    else
    {
        printf("查询价格失败: %s\n", sqlite3_errmsg(pd));
        sqlite3_finalize(stmt);
        return -1;
    }

    // 清理SQL语句
    sqlite3_finalize(stmt);

    return 1;
}

int showMerchant_Sql(QUREY_RST *pQR, char *s)
{
    char table_name[20];
    strcpy(table_name, s);
    char show_sql[256];
    // 构造SQL查询语句，仅选择Name列
    snprintf(show_sql, sizeof(show_sql), "SELECT Name FROM %s;", table_name);

    // 执行SQL查询
    int rc = sqlite3_get_table(pd, show_sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);

    // 检查查询是否成功
    if (rc != SQLITE_OK)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", show_sql, errmsg);
        sqlite3_free(errmsg); // 释放错误消息
        return -1;            // 查询失败
    }

    // 返回查询结果的行数和列数
    return 1; // 查询成功
}

// 执行查询的SQL语句
int querySql(QUREY_RST *pQR, RGU *pRgu, char *s)
{
    // sqlite3_get_table获取数据库表格的内容，通常用来执行数据库查询SQL语句
    // 参数
    // 1. 数据库的指针
    // 2. 要执行的SQL查询语句
    // 3. 查询的结果
    // 4. 查询结果的行数
    // 5. 查询结果的列数
    // 6. 错误信息
    char table_name[20];
    strcpy(table_name, s);
    char sql[256];
    sprintf(sql, "select * from %s where Name = '%s' AND Password = '%s';", table_name, pRgu->name, pRgu->pwd);

    int data = sqlite3_get_table(pd, sql, &pQR->pzResult, &pQR->row, &pQR->col, &errmsg);

    if (errmsg != NULL)
    {
        printf("查询数据库失败，SQL语句是：%s 原因是：%s\n", sql, errmsg);
        sqlite3_free(errmsg);
        errmsg = NULL;
        return -1;
    }
    else
    {
        return 1;
        // printf("row = %d col = %d result = %s\n", row, col, pzResult[1 + 4]);

        // for (size_t i = 0; i < row * col; i++)
        // {
        //      // 打印查询的结果，+4是为了跳过结果的表头
        //      printf("%s  ", pzResult[i + 4]);
        // }
        // printf("\n");
    }
}
